package com.handpay.zztong.hp.paymentbill.model.revoke;

import com.handpay.framework.LuaTableUtil;
import com.handpay.framework.fastjson.FastJsonHelper;
import com.handpay.framework.utils.CommonUtils;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.base.callback.BusinessEngineListener;
import com.handpay.zztong.hp.base.model.base.BaseModel;
import com.handpay.zztong.hp.bean.Rates;
import com.handpay.zztong.hp.bean.ResponseBean;
import com.handpay.zztong.hp.config.HttpUrlConstant;

import java.util.Hashtable;

import se.krka.kahlua.vm.LuaTable;

/**
 * Created by fplei on 2017/5/2.
 * 撤销
 */

public class RevokeModle extends BaseModel {
    private BusinessEngineListener<RevokeBean> mBusinessEngineListener;

    public boolean doRevoketran(ZZTongActivity activity, Hashtable<String, String> params, BusinessEngineListener listener) {
        mBusinessEngineListener = listener;
        return doPost(activity, HttpUrlConstant.Do_zztRevokeTransaction, params, true, true);
    }

    @Override
    public boolean netResponse(String action, byte[] data) {
        boolean bResult = super.netResponse(action, data);
        if (bResult) {
            return true;
        }
        if (action.equals(HttpUrlConstant.Do_zztRevokeTransaction)) {
            RevokeBean bean = new RevokeBean();
            if (data_type == DATA_TYPE.DATA_TYPE_JSON) {
                try {
                    String result = new String(data, "UTF-8");
                    bean = FastJsonHelper.getObject(result, RevokeBean.class);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                LuaTable luaTable = LuaTableUtil.stringToLuaTable(data, true);
                // 网络超时会返回Null需要判断
                if (luaTable == null) {
					/*
					 * if (super.netResponse(action, resp, showAlert, on)) { return
					 * true; }
					 */
                    return true;
                }
                Object obj = luaTable.rawget("status");
                int status=CommonUtils.objectToInt(luaTable.rawget("status"));
                String failMessage="";
                //撤销失败则获取失败信息
                if(status==2){
                    failMessage=(String)luaTable.rawget("message");
                }
                bean.setStatus(status);
                bean.setMessage(failMessage);
            }
            if (mBusinessEngineListener != null) {
                mBusinessEngineListener.callbackBusinessResponse(bean);
            }
        }
        return true;
    }
}
